package com.njuse.jvmfinal.Instruction;

import java.nio.ByteBuffer;

import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.RuntimeConstantPool;
import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.Constant;
import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.wrapper.FloatWrapper;
import com.njuse.jvmfinal.memory.jclass.runtimeConstantPool.constant.wrapper.IntWrapper;
import com.njuse.jvmfinal.runtimestructure.Jframe;

public class LDC extends Instruction {
	private int index;
	
	public  void readindex(ByteBuffer reader) {
		this.index= (int) reader.get();
	}
	
	public void execute(Jframe frame) {
		int number;
		RuntimeConstantPool rtcp=frame.getMymethod().getClazz().getRuntimeConstantPool();
		Constant temp=rtcp.getConstant(index);
		if (temp.getClass().getName().contains("Float")) {
			FloatWrapper floattemp=(FloatWrapper) temp;
			  number=Float.floatToIntBits(floattemp.getValue());
		}else {
			IntWrapper inttemp=(IntWrapper)temp;
		      number=inttemp.getValue();
		}
		Object[] stack=frame.getManagestack();
		int top=frame.getStacktop();
		stack[top]= number;
		frame.setStacktop(top+1);
		frame.setManagestack(stack);
		
	}
}
